package roborally;

import be.kuleuven.cs.som.annotate.*;
import Exceptions.ElementPlacementError;
import Exceptions.IllegalCoordinateException;
import Exceptions.TerminationError;

/**
 * @author Jef Keijers/Bachelor of Science: Informatics
 * @version 1.0
 */
public abstract class Item extends Element {

	/**
	 * @param 	weight
	 * @effect	
	 * 			|super()
	 * @post
	 * 			| if (weight < 0) then new.getWeight == 0
	 * 			| if (weight > Integer.MAX_VALUE) then new.getWeight == Integer.MAX_VALUE
	 * 			| if (weight => 0 && weight <= Integer.MAX_VALUE) then new.getWeight == weight
	 */
	public Item (int weight) {
		super();
		if (weight > Integer.MAX_VALUE)
			this.weight = Integer.MAX_VALUE;
		else if (weight<0)
			this.weight=0;
		else
			this.weight = weight;
	}
	
	@Basic
	public int getWeight() {
		return weight;
	}

	public void setWeight(int weight) {
		this.weight=weight;
	}
	
	/**
	 * @param 	weight
	 * @return | if ((weight>=0) && (weight<=Integer.MAX_VALUE)) then result == true
	 */
	public boolean canHaveAsWeight(int weight){
		if ((weight>=0) && (weight<=Integer.MAX_VALUE))
			return true;
		return false; 
	}

	protected int weight=0;

	/**
	 * @param 	board
	 * @param 	x
	 * @param 	y
	 * @throws 	IllegalCoordinateException
	 * 			|if ( ! Position.canHaveAsXCoordinate(x,board) || ! Position.canHaveAsYCoordinate(y,board) ) then throw new IllegalCoordinateException()
	 * @throws 	ElementPlacementError 
	 * 			|board.putElementOnPosition(this, position);		
	 * @effect	
	 * 			|board.putElementOnPosition(this, position);			

	 */
	public void putItem(Board board, long x, long y) throws IllegalCoordinateException, ElementPlacementError {
		if (this.getBoard()==null) {					
			Position position=new Position(x, y, board);
			board.putElementOnPosition(this, position);			
		} 
	}

	public abstract void handleUse(Robot robot) throws TerminationError, IllegalCoordinateException, ElementPlacementError;

	public abstract void handleShot() throws IllegalCoordinateException, TerminationError;
}
